package com.fy.market.strategy.service.assemble.template;

import com.fy.market.strategy.model.entity.StrategyAwardRuleEntity;
import com.fy.market.strategy.model.entity.StrategyRuleEntity;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.assemble.IStrategyAwardAssemble;
import com.fy.market.types.common.reslut.R;
import com.fy.market.types.common.constant.ResultMessageConstant;
import com.fy.market.types.common.ex.ExHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

import static com.fy.market.types.common.reslut.R.ok;

/**
 * 策略奖品仓库实现类
 *
 * @author fwq
 * @date 2025-07-24 10:00
 */
@Slf4j
public abstract class StrategyAwardAssembleTmp implements IStrategyAwardAssemble {

    private IStrategyRepository iStrategyRepository;


    public StrategyAwardAssembleTmp(
            IStrategyRepository iStrategyRepository) {
        this.iStrategyRepository = iStrategyRepository;
    }

    /**
     * 查询策略奖品列表，并根据奖品概率生成包含奖品 ID 的列表，将结果存入缓存
     *
     * @param strategyId 策略 ID，用于唯一标识一个抽奖策略
     * @return 操作成功的响应对象
     */
    @Override
    public R assembleStrategyAward(Integer strategyId) {

        // 校验策略是否存在
        ExHandler.throwIf(iStrategyRepository.getStrategyEntity(strategyId) == null, ResultMessageConstant.LotteryStrategy.STRATEGY_NOT_FOUND);

        // 根据策略ID获取策略奖品合集
        List<StrategyAwardRuleEntity> strategyAwardEntities = iStrategyRepository.queryStrategyAwardRuleEntities(strategyId);

        // 校验策略奖品是否存在
        ExHandler.throwIf(strategyAwardEntities == null || strategyAwardEntities.isEmpty(), ResultMessageConstant.LotteryStrategy.STRATEGY_AWARD_LIST_EMPTY);

        // 根据策略ID获取策略奖品规则
        List<StrategyRuleEntity> strategyRuleEntities = iStrategyRepository.queryStrategyRuleList(strategyId);

        // 执行所有装配钩子方法

        // 执行默认装配钩子方法
        this.doDefaultAssemble(strategyId, strategyAwardEntities, strategyRuleEntities);

        // 执行权重装配钩子方法
        this.doRuleWeightAssemble(strategyId, strategyAwardEntities, strategyRuleEntities);

        // 执行策略奖品库存装配钩子方法
        this.doStrategyAwardStockAssemble(strategyId);

        // 后续扩展的钩子方法可以在这里添加
        // this.doTimeLimitAssemble(strategyId, strategyAwardEntities, strategyRuleEntities);
        // this.doUserLevelAssemble(strategyId, strategyAwardEntities, strategyRuleEntities);

        return ok();
    }

    /**
     * 默认装配钩子方法
     */
    protected abstract void doDefaultAssemble(Integer strategyId, List<StrategyAwardRuleEntity> strategyAwardRuleEntities, List<StrategyRuleEntity> strategyRuleEntities);

    /**
     * 权重装配钩子方法
     */
    protected abstract void doRuleWeightAssemble(Integer strategyId, List<StrategyAwardRuleEntity> strategyAwardRuleEntities, List<StrategyRuleEntity> strategyRuleEntities);
    /**
     * 策略奖品库存装配钩子方法
     */
    protected abstract void doStrategyAwardStockAssemble(Integer strategyId);

    // 后续扩展的钩子方法可以在这里添加
    // protected abstract void doTimeLimitAssemble(Integer strategyId, List<StrategyAwardRuleEntity> strategyAwardRuleEntities, List<StrategyRuleEntity> strategyRuleEntities);
    // protected abstract void doUserLevelAssemble(Integer strategyId, List<StrategyAwardRuleEntity> strategyAwardRuleEntities, List<StrategyRuleEntity> strategyRuleEntities);
}
